package com.iot08.push.mob;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import mob.push.api.config.MobPushConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @version 1.0
 * @Description: mob配置初始化
 * @time: 2020/12/30 18:07
 * @author: Rock
 */
@Slf4j
@Configuration
public class MobConfig {

    private String appKey;

    private String appSecret;

    private String packageName;

    @Value("${mob.push.tcsh_key}")
    private String tcsh_appKey;

    @Value("${mob.push.tcsh_secret}")
    private String tcsh_appSecret;

    @Value("${mob.android.add_scheme:}")
    public String MOB_COMMON_SCHEME;
    @Value("${mob.iOS.scheme:}")
    public String MOB_IOS_COMMON_SCHEME;

    @Value("${mob.android.tcsh_add_scheme:}")
    public String TCSH_MOB_COMMON_SCHEME;
    @Value("${mob.iOS.tcsh_scheme:}")
    public String TCSH_MOB_IOS_COMMON_SCHEME;

    @Value("${mob.push.configMap:}")
    public String MOB_PUSH_CONFIGStr;

    final public static String PACKAGENAME = "packageName";
    final public static String LASTVERSION_ANDROID = "lastVersion_android";
    final public static String LASTVERSION_IOS = "lastVersion_ios";
    final public static String MOBKEY = "mobKey";
    final public static String MOBSECRET = "mobSecret";
    final public static String ANDROID_SCHEME = "android_scheme";
    final public static String IOS_SCHEME = "ios_scheme";


    /**
     * Mob的配置
     * {
     * 	"com.jianyou.tongxin": {
     * 		"packageName": "com.jianyou.tongxin",
     * 		"lastVersion_android": "1.4.2",
     * 		"lastVersion_ios": "1.5.0",
     * 		"mobKey": "XXX",
     * 		"mobSecret": "XXX",
     * 		"android_scheme": "tongxin://tongxin/transfer_page",
     * 		"ios_scheme": "http://XXXXXX"
     *        },
     * 	"com.jh.watch": {
     * 		"packageName": "com.jh.watch",
     * 		"lastVersion_android": "1.4.4",
     * 		"lastVersion_ios": "1.2.7",
     * 		"mobKey": "XXX",
     * 		"mobSecret": "XXX",
     * 		"android_scheme": "tcsh://tcsh/transfer_page",
     * 		"ios_scheme": "http://XXXXXX"
     *    }
     * }
     */
    public static JSONObject mobPushConfigMap;



    /**
     * @time: 2020/12/30 18:10
     * @author: Rock
     * @version 1.0
     */
    @Bean
    public void initConfig() {
        if(MOB_PUSH_CONFIGStr.length() > 0){
            mobPushConfigMap = JSONObject.parseObject(MOB_PUSH_CONFIGStr);
            //默认取一个
            for (String key: mobPushConfigMap.keySet()) {
                JSONObject item = mobPushConfigMap.getJSONObject(key);
                String tempKey = item.getString(MOBKEY);
                String tempSecret = item.getString(MOBSECRET);
                String tempPackageName = item.getString(PACKAGENAME);
                if(!StringUtils.isEmpty(tempKey) && !StringUtils.isEmpty(tempSecret)){
                    MobPushConfig.appkey = tempKey;
                    MobPushConfig.appSecret = tempSecret;
                    appKey = tempKey;
                    appSecret = tempSecret;
                    packageName = tempPackageName;
                   break;
                }
            }
            log.info("默认取一个packageName={} MobPushConfig.appkey={} MobPushConfig.appSecret={} mobPushConfigMap={}",
                    packageName, MobPushConfig.appkey, MobPushConfig.appSecret, mobPushConfigMap);

        }else{
            mobPushConfigMap = null;
        }
    }

    /**
     * 根据包名切换MobKey的设置 并返回对应的 scheme 头
     * @param packageName 包名
     * @param platform 1=安卓；2=ios
     * @return
     */
    public String setConfigWithPackageNameAndGetScheme(String packageName, Integer platform) {
        if (!StringUtils.isEmpty(packageName)) {
            if(mobPushConfigMap != null && mobPushConfigMap.keySet().contains(packageName)){
                JSONObject item = mobPushConfigMap.getJSONObject(packageName);
                String tempKey = item.getString(MOBKEY);
                String tempSecret = item.getString(MOBSECRET);
                String tempPackageName = item.getString(PACKAGENAME);
                String tempScheme1 = item.getString(ANDROID_SCHEME);
                String tempScheme2 = item.getString(IOS_SCHEME);
                if(!StringUtils.isEmpty(tempKey) && !StringUtils.isEmpty(tempSecret)){
                    MobPushConfig.appkey = tempKey;
                    MobPushConfig.appSecret = tempSecret;
                    appKey = tempKey;
                    appSecret = tempSecret;
                    packageName = tempPackageName;
                    return (1==platform) ? tempScheme1 : tempScheme2;
                }
            }else{
                log.error("packageName={} 在mobPushConfigMap中找不到", packageName);
            }
//            if ("com.jianyou.tongxin".equalsIgnoreCase(packageName)) {
//                //赛诺
//                if (!MobPushConfig.appkey.equalsIgnoreCase(appKey) || !MobPushConfig.appSecret.equalsIgnoreCase(appSecret)) {
//                    log.info("切换成赛诺的mobKey");
//                    MobPushConfig.appkey = appKey;
//                    MobPushConfig.appSecret = appSecret;
//                }
//                return (1==platform) ? MOB_COMMON_SCHEME : MOB_IOS_COMMON_SCHEME;
//            }else if ("com.jh.watch".equalsIgnoreCase(packageName)) {
//                //e路相伴
//                if (!MobPushConfig.appkey.equalsIgnoreCase(tcsh_appKey) || !MobPushConfig.appSecret.equalsIgnoreCase(tcsh_appSecret)) {
//                    log.info("切换成e路相伴的mobKey");
//                    MobPushConfig.appkey = tcsh_appKey;
//                    MobPushConfig.appSecret = tcsh_appSecret;
//                }
//                return (1==platform) ? TCSH_MOB_COMMON_SCHEME : TCSH_MOB_IOS_COMMON_SCHEME;
//            }
        }
        return null;
    }
}
